
package inf3791.calendarsystem.shared;

import java.util.Calendar;
import java.util.Date;

/**
 * Represents a single day in a calendar.
 * 
 * @author Jonas Weber
 */
public class DaySpec {
    public final int day;
    public final int month;
    public final int year;

    private final int hash;
    
    public DaySpec(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
        
        StringBuilder sb = new StringBuilder();
        sb.append(year).append("-").append(month).append("-").append(day);
        
        hash = sb.toString().hashCode();
    }

    @Override
    public int hashCode() {
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final DaySpec other = (DaySpec) obj;
        if (this.day != other.day) {
            return false;
        }
        if (this.month != other.month) {
            return false;
        }
        if (this.year != other.year) {
            return false;
        }
        return true;
    }
    
    public Date toDate()
    {
        Calendar c = Calendar.getInstance();
        
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DATE, day);

        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        
        return c.getTime();
    }
}
